﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZK.BLL;
using ZK.Entity;
using ZK.MyControl;

namespace ZK.Client.WinForm
{
    public partial class FrmCIJTemplate : Form
    {
        public FrmCIJTemplate()
        {
            InitializeComponent();
        }
        BindingList<CIJTemplate> bindinglist = new BindingList<CIJTemplate>();
        BindingSource bindingSource = new BindingSource();
        private void FrmCIJTemplate_Load(object sender, EventArgs e)
        {
            bindingSource.DataSource = bindinglist;
            dataView1.DataSource = bindingSource;
            AddCloumns();
            LoadData();
        }



        private void AddCloumns()
        {

            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.TemplateName), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P1Font), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P1Unit), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P1OffSetY), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P1OffSetX), true));


            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P2Font), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P2Unit), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P2OffSetY), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P2OffSetX), true));


            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P3Font), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P3Unit), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P3OffSetY), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(CIJTemplate), nameof(CIJTemplate.P3OffSetX), true));

        }


        private void LoadData()
        {
            var list = CIJTemplateBLL.Instance.GetList();
            bindinglist.Clear();
            if (list != null)
            {
                foreach (var item in list)
                {
                    bindinglist.Add(item);
                }
            }

        }



        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dataView1.CurrentCell != null && dataView1.CurrentCell.RowIndex > -1)
            {
                if (MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    if (CIJTemplateBLL.Instance.DeleteById(bindinglist[dataView1.CurrentCell.RowIndex].Id))
                    {
                        bindinglist.RemoveAt(dataView1.CurrentCell.RowIndex);
                    }
                }

            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmCIJTemplateEdit frm = new FrmCIJTemplateEdit();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dataView1.CurrentCell != null && dataView1.CurrentCell.RowIndex > -1)
            {
                FrmCIJTemplateEdit frm = new FrmCIJTemplateEdit();
                frm.CIJTemplate = bindinglist[dataView1.CurrentCell.RowIndex];
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }
    }

}
